home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / sendmail.8.8.4.tar.gz / sendmail.8.8.4.tar / sendmail-8.8.4 / contrib / etrn.pl < prev    next >
Perl Script  |  1996-11-22  |  8KB  |  302 lines

  1. #!/usr/local/bin/perl
  2. 'di ';
  3. 'ds 00 \\"';
  4. 'ig 00 ';
  5. #
  6. #       THIS PROGRAM IS ITS OWN MANUAL PAGE.  INSTALL IN man & bin.
  7. #
  8.  
  9. # hardcoded constants, should work fine for BSD-based systems
  10. require 'sys/socket.ph';
  11. $sockaddr = 'S n a4 x8';
  12.  
  13. # system requirements:
  14. #     must have 'hostname' program.
  15.  
  16. #############################################################################
  17. #  Copyright (c) 1996 John T. Beck <john@beck.org>
  18. #  All rights reserved.
  19. #
  20. #  Redistribution and use in source and binary forms, with or without
  21. #  modification, are permitted provided that the following conditions
  22. #  are met:
  23. #  1. Redistributions of source code must retain the above copyright
  24. #     notice, this list of conditions and the following disclaimer.
  25. #  2. Redistributions in binary form must reproduce the above copyright
  26. #     notice, this list of conditions and the following disclaimer in the
  27. #     documentation and/or other materials provided with the distribution.
  28. #  3. All advertising materials mentioning features or use of this software
  29. #     must display the following acknowledgement:
  30. #       This product includes software developed by John T. Beck.
  31. #  4. The name of John Beck may not be used to endorse or promote products
  32. #     derived from this software without specific prior written permission.
  33. #
  34. #  THIS SOFTWARE IS PROVIDED BY JOHN T. BECK ``AS IS'' AND ANY EXPRESS OR
  35. #  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  36. #  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  37. #  IN NO EVENT SHALL JOHN T. BECK BE LIABLE FOR ANY DIRECT, INDIRECT,
  38. #  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. #  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  40. #  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  41. #  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  42. #  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  43. #  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. #
  45. #  This copyright notice derived from material copyrighted by the Regents
  46. #  of the University of California.
  47. #
  48. #  Contributions accepted.
  49. #############################################################################
  50. #  Further disclaimer: the etrn.pl script was highly leveraged from the
  51. #  expn.pl script which is (C) 1993 David Muir Sharnoff.
  52. #############################################################################
  53.  
  54. $port = 'smtp';
  55. $av0 = $0;
  56. select(STDERR);
  57.  
  58. $0 = "$av0 - running hostname";
  59. chop($name = `hostname || uname -n`);
  60.  
  61. $0 = "$av0 - lookup host FQDN and IP addr";
  62. ($hostname,$aliases,$type,$len,$thisaddr) = gethostbyname($name);
  63.  
  64. push(@hosts,$hostname);
  65.  
  66. $0 = "$av0 - parsing sendmail.cf";
  67. open(CF, "</etc/sendmail.cf") || die "open /etc/sendmail.cf: $!";
  68. while (<CF>){
  69.     if (/^Fw.*$/){            # look for a line starting with "Fw"
  70.         $cwfile = $_;
  71.         chop($cwfile);
  72.         $optional = /^Fw-o/;
  73.         $cwfile =~ s,^Fw[^/]*,,;    # extract the file name
  74.     }
  75. }
  76. close(CF);
  77.  
  78. $0 = "$av0 - reading $cwfile";
  79. if (open(CW, "<$cwfile")){
  80.     while (<CW>){
  81.         $thishost = $_;
  82.         chop($thishost);
  83.         push(@hosts, $thishost) unless $thishost =~ $hostname;
  84.     }
  85.     close(CW);
  86. } else {
  87.         die "open $cwfile: $!" unless optional;
  88. }
  89.  
  90. $0 = "$av0 - parsing args";
  91. $usage = "Usage: $av0 [-wd] host";
  92. for $a (@ARGV) {
  93.     die $usage if $a eq "-";
  94.     while ($a =~ s/^(-.*)([wd])/$1/) {
  95.         eval '$'."flag_$2 += 1";
  96.     }
  97.     next if $a eq "-";
  98.     die $usage if $a =~ /^-/;
  99.     $server = $a;
  100. }
  101. $watch = $flag_w;
  102. $debug = $flag_d;
  103.  
  104. die $usage unless $server;
  105.  
  106. $0 = "$av0 - building local socket";
  107. ($name,$aliases,$proto) = getprotobyname('tcp');
  108. ($name,$aliases,$port) = getservbyname($port,'tcp')
  109.     unless $port =~ /^\d+/;
  110. $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
  111.  
  112. # look it up
  113. $0 = "$av0 - gethostbyname($server)";
  114.  
  115. ($name,$aliases,$type,$len,$thataddr) = gethostbyname($server);
  116.                 
  117. # get a connection
  118. $0 = "$av0 - socket to $server";
  119. $that = pack($sockaddr, &AF_INET, $port, $thataddr);
  120. socket(S, &AF_INET, &SOCK_STREAM, $proto)
  121.     || die "socket: $!";
  122. $0 = "$av0 - bind to $server";
  123. bind(S, $this) 
  124.     || die "bind $hostname,0: $!";
  125. $0 = "$av0 - connect to $server";
  126. print "debug = $debug server = $server\n" if $debug > 8;
  127. if (! connect(S, $that)) {
  128.     $0 = "$av0 - $server: could not connect: $!\n";
  129. }
  130. select((select(S),$| = 1)[0]); # don't buffer output to S
  131.  
  132. # read the greeting
  133. $0 = "$av0 - talking to $server";
  134. &alarm("greeting with $server",'');
  135. while(<S>) {
  136.     alarm(0);
  137.     print if $watch;
  138.     if (/^(\d+)([- ])/) {
  139.         if ($1 != 220) {
  140.             $0 = "$av0 - bad numeric response from $server";
  141.             &alarm("giving up after bad response from $server",'');
  142.             &read_response($2,$watch);
  143.             alarm(0);
  144.             print STDERR "$server: NOT 220 greeting: $_"
  145.                 if ($debug || $watch);
  146.         }
  147.         last if ($2 eq " ");
  148.     } else {
  149.         $0 = "$av0 - bad response from $server";
  150.         print STDERR "$server: NOT 220 greeting: $_"
  151.             if ($debug || $watch);
  152.         close(S);
  153.     }
  154.     &alarm("greeting with $server",'');
  155. }
  156. alarm(0);
  157.     
  158. # if this causes problems, remove it
  159. $0 = "$av0 - sending helo to $server";
  160. &alarm("sending ehlo to $server","");
  161. &ps("ehlo $hostname");
  162. $etrn_support = 0;
  163. while(<S>) {
  164.     if (/^250([- ])ETRN(.+)$/){
  165.         $etrn_support = 1;
  166.     }
  167.     print if $watch;
  168.     last if /^\d+ /;
  169. }
  170. alarm(0);
  171.  
  172. if ($etrn_support){
  173.     print "ETRN supported\n" if ($debug)
  174.     &alarm("sending etrn to $server",'');
  175.     while (@hosts) {
  176.         $server = shift(@hosts);
  177.         &ps("etrn $server");
  178.         while(<S>) {
  179.             print if $watch;
  180.             last if /^\d+ /;
  181.         }
  182.         sleep(1);
  183.     }
  184. } else {
  185.     print "\nETRN not supported\n\n"
  186. }
  187.  
  188. &alarm("sending 'quit' to $server",'');
  189. $0 = "$av0 - sending 'quit' to $server";
  190. &ps("quit");
  191. while(<S>) {
  192.     print if $watch;
  193.     last if /^\d+ /;
  194. }
  195. close(S);
  196. alarm(0);
  197.  
  198. select(STDOUT);
  199. exit(0);
  200.  
  201. # print to the server (also to stdout, if -w)
  202. sub ps
  203. {
  204.     local($p) = @_;
  205.     print ">>> $p\n" if $watch;
  206.     print S "$p\n";
  207. }
  208.  
  209. sub alarm
  210. {
  211.     local($alarm_action,$alarm_redirect,$alarm_user) = @_;
  212.     alarm(3600);
  213.     $SIG{ALRM} = 'handle_alarm';
  214. }
  215.  
  216. sub handle_alarm
  217. {
  218.     &giveup($alarm_redirect,"Timed out during $alarm_action",$alarm_user);
  219. }
  220.  
  221. # read the rest of the current smtp daemon's response (and toss it away)
  222. sub read_response
  223. {
  224.     local($done,$watch) = @_;
  225.     local(@resp);
  226.     print $s if $watch;
  227.     while(($done eq "-") && ($s = <S>) && ($s =~ /^\d+([- ])/)) {
  228.         print $s if $watch;
  229.         $done = $1;
  230.         push(@resp,$s);
  231.     }
  232.     return @resp;
  233. }
  234. # to pass perl -w:
  235. @tp;
  236. $flag_a;
  237. $flag_d;
  238. &handle_alarm;
  239. ################### BEGIN PERL/TROFF TRANSITION 
  240. .00 ;    
  241.  
  242. 'di
  243. .nr nl 0-1
  244. .nr % 0
  245. .\\"'; __END__ 
  246. .\" ############## END PERL/TROFF TRANSITION
  247. .TH ETRN 1 "November 16, 1996"
  248. .AT 3
  249. .SH NAME
  250. etrn \- start mail queue run
  251. .SH SYNOPSIS
  252. .B etrn
  253. .RI [ -w ]
  254. .RI [ -d ]
  255. .IR hostname
  256. .SH DESCRIPTION
  257. .B etrn
  258. will use the SMTP
  259. .B etrn
  260. command to start mail delivery from the host given on the command line.
  261. .SH OPTIONS
  262. .LP
  263. The normal mode of operation for
  264. .B etrn
  265. is to do all of its work silently.
  266. The following options make it more verbose.
  267. It is not necessary to make it verbose to see what it is
  268. doing because as it works, it changes its 
  269. .BR argv [0]
  270. variable to reflect its current activity.
  271. The 
  272. .IR -w ,
  273. watch, flag will cause
  274. .B etrn
  275. to show you its conversations with the mail daemons.
  276. The 
  277. .IR -d ,
  278. debug, flag will expose many of the inner workings so that
  279. it is possible to eliminate bugs.
  280. .SH ENVIRONMENT
  281. No enviroment variables are used.
  282. .SH FILES
  283. .B /etc/sendmail.cf
  284. .SH SEE ALSO
  285. .BR sendmail (8),
  286. RFC 1985.
  287. .SH BUGS
  288. Not all mail daemons will implement 
  289. .B etrn .
  290. .LP
  291. It is assumed that you are running domain names.
  292. .SH CREDITS
  293. Leveraged from David Muir Sharnoff's expn.pl script.
  294. .SH AVAILABILITY
  295. The latest version of 
  296. .B etrn
  297. is available in the contrib directory of the sendmail
  298. distribution through anonymous ftp at
  299. .IR ftp://ftp.sendmail.org/ucb/src/sendmail/ .
  300. .SH AUTHOR
  301. .I John T. Beck\ \ \ \ <john@beck.org>
  302.